package scu.maqiang.numeric;

import java.sql.Array;
import java.util.ArrayList;
import java.util.Arrays;

public class CSCMatrix {
    public CSCMatrix(SRMatrix mat) {
        m = mat.getM();
        n = mat.getN();
        nnz = mat.getNNZ();
        ArrayList<ArrayList<Pair<Integer, Double>>> cols = new ArrayList<>();
        for(int i = 0; i < n; i++) {
            cols.add(new ArrayList<>());
        }
        for(int i = 0; i < m; i++)  {
            for(SREntry ele: mat.mat[i]) {
                cols.get(ele.col).add(new Pair<Integer, Double>(i, ele.val));
            }
        }
        for(int i = 0; i < n; i++) {
            cols.get(i).sort((a, b) -> a.first > b.first ? 1 : -1);
        }
        colIndex = new int[n + 1];
        rows = new int[nnz];
        values = new double[nnz];
        int idx = 0;
        for(int i = 0; i < n; i++) {
            colIndex[i + 1] = colIndex[i] + cols.get(i).size();
            for(Pair<Integer, Double> ele: cols.get(i)) {
                rows[idx] = ele.getFirst();
                values[idx++] = ele.getSecond();
            }
        }
        isSymmetric = mat.isSymmetric();
    }
    @Override
    public String toString() {
        StringBuilder sb = new StringBuilder();
        sb.append(m + "\t" + n + "\t" + nnz + "\t" + isSymmetric +"\n");
        sb.append(Arrays.toString(colIndex) + "\n");
        sb.append(Arrays.toString(rows) + "\n");
        sb.append(Arrays.toString(values) + "\n");
        return sb.toString();
    }

    //稀疏矩阵列指标
    private int[] colIndex;
    //稀疏矩阵行指标
    private int[] rows;
    //稀疏矩阵元素值
    private double[] values;
    //稀疏矩阵行数
    private int m;
    //稀疏矩阵列数
    private int n;
    //稀疏矩阵非0元素
    private int nnz;
    //稀疏矩阵是否对称
    private boolean isSymmetric;
}
